package com.fsy.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by shuangyu.feng on 2017/8/8.
 */
public class SingleLink {

	static class ListNode {
		Integer val;
		ListNode next;

		ListNode(Integer x) {
			val = x;
		}
	}

	public static ListNode reverseList1(ListNode head) {
		ListNode prev = null;
		while(head!=null){
			ListNode tmp = head.next;
			head.next = prev;
			prev = head;
			head = tmp;
		}
		return prev;
	}

	public static ListNode reverseList2(ListNode head) {
		if(head==null||head.next ==null)
			return head;
		ListNode prev = reverseList2(head.next);
		head.next.next = head;
		head.next = null;
		return prev;
	}


	public static void main(String[] args){
		ListNode listNode = new ListNode(0);

		for(int i = 1;i< 10 ;i++){
			ListNode e,p;
			p = listNode;
			while ((e = p.next) != null){

				p = e;
			}
			p.next = new ListNode(i);
		}
//		printMessage1(listNode);
		printMessage(listNode);
//
//		listNode = reverseList1(listNode);
//		printMessage(listNode);
//
		listNode = reverseList2(listNode);
		printMessage(listNode);
	}

	public static void printMessage(ListNode listNode){
		if(listNode.next != null){
			System.out.print(listNode.val + "");
			printMessage(listNode.next);
		}else{
			System.out.print(listNode.val + "");
			System.out.println();
		}

	}
	public static void printMessage1(ListNode listNode){
		if(listNode == null) {
			System.out.println();
			return;
		}

		printMessage1(listNode.next);
		System.out.print(listNode.val);
	}
}
